;
var helper = require("../../javascript/helper");
var db = require("../../javascript/mysql");
var request = require("request");

var mysql = new db.mysqlClient("accident");
var url = "https://sales.allianz.com.cn/emall/ebiz/websiteprt/insDetail.action?action=ajaxCalcPrem&ajax=true";
// 生成产品数据
function genProductInfo() {
    var projects = [];
    plan = {
        "planA": {
            "year": ["0-70"],
            "days": ["1d", "7d", "15d", "1m", "3m", "6m", "9m", "1y"],
            "people_count": ["1"],
            "accident_death": ["200000"],
            "accident_medical": ["10000"],
            "accident_hospital": ["50/天"],
            "myselfDriver": [""],
            "train_death": [""],
            "accident_nature": [""],
            "bus_death": [""],
            "elvator_death": [""]

        },
        "planB": {
            "year": ["0-70"],
            "days": ["1d", "7d", "15d", "1m", "3m", "6m", "9m", "1y"],
            "people_count": ["1"],
            "accident_death": [""],
            "accident_medical": ["10000"],
            "accident_hospital": ["50/天"],
            "myselfDriver": ["100000"],
            "train_death": ["200000"],
            "accident_nature": ["100000"],
            "bus_death": [""],
            "elvator_death": [""]

        },
        "planC": {
            "year": ["0-70"],
            "days": ["1d", "7d", "15d", "1m", "3m", "6m", "9m", "1y"],
            "people_count": ["1"],
            "accident_death": [""],
            "accident_medical": ["10000"],
            "accident_hospital": ["50/天"],
            "myselfDriver": ["200000"],
            "train_death": [""],
            "accident_nature": [""],
            "bus_death": [""],
            "elvator_death": [""]

        },
        "planD": {
            "year": ["0-70"],
            "days": ["1d", "7d", "15d", "1m", "3m", "6m", "9m", "1y"],
            "people_count": ["1"],
            "accident_death": [""],
            "accident_medical": ["10000"],
            "accident_hospital": ["50/天"],
            "myselfDriver": ["100000"],
            "train_death": ["100000"],
            "accident_nature": [""],
            "bus_death": ["100000"],
            "elvator_death": ["50000"]

        }
    }
    for (k in plan) {
        data = helper.product([
            plan[k]["year"], plan[k]["days"], plan[k]["people_count"], plan[k]["accident_death"], plan[k]["accident_medical"], plan[k]["accident_hospital"], plan[k]["myselfDriver"], plan[k]["train_death"], plan[k]["accident_nature"], plan[k]["bus_death"], plan[k]["elvator_death"]
        ])
        for (i = 0; i < data.length; i++) {
            dt = helper.formatProductInfo(data[i], plan[k]);
            projects.push(dt);
        }
    }
    return projects;

}
function httpRequest(dt) {
    //判断保障时间
    if (dt.days.indexOf("d") != -1) {
        endDate = dt.days.replace("d", "")
    } else if (dt.days.indexOf("m") != -1) {
        endDate = (Number(dt.days.replace("m", "")) * 30).toString();
    } else {
        endDate = "365";
    }
    //判断投保计划
    var stringProjects = "";
    for (k in dt) {
        if (k === "accident_death" && dt.accident_death.length != 0) {
            stringProjects += '{"dutyCode":"ADD","dutyAmt":"' + dt.accident_death + '"}' + ","
        } else if (k === "accident_medical" && dt.accident_medical.length != 0) {
            stringProjects += '{"dutyCode":"AMR","dutyAmt":"' + dt.accident_medical + '"}' + ","

        } else if (k === "accident_hospital" && dt.accident_hospital.length != 0) {
            stringProjects += '{"dutyCode":"AHI","dutyAmt":"' + dt.accident_hospital.replace("/天", "") + '"}' + ","

        } else if (k === "myselfDriver" && dt.myselfDriver.length != 0) {
            stringProjects += '{"dutyCode":"ADDPS","dutyAmt":"' + dt.myselfDriver + '"}' + ","
        } else if (k === "train_death" && dt.train_death.length != 0) {
            stringProjects += '{"dutyCode":"ADDT","dutyAmt":"' + dt.train_death.length + '"}' + ","

        } else if (k === "accident_nature" && dt.accident_nature.length != 0) {
            stringProjects += '{"dutyCode":"NDR","dutyAmt":"' + dt.accident_nature + '"}' + ","
        } else if (k === "bus_death" && dt.bus_death.length != 0) {
            stringProjects += '{"dutyCode":"ADDC","dutyAmt":"' + dt.bus_death + '"}' + ","
        } else if (k === "elvator_death" && dt.elvator_death.length != 0) {
            stringProjects += '{"dutyCode":"ELEVATOR","dutyAmt":"' + dt.elvator_death + '"}' + ","
        } else {

        }
    }
    stringProjects = stringProjects.substring(0, stringProjects.lastIndexOf(","));

    params = {
        "ebizOrderDto.productCode": "YW0006",
        "ebizAppntDto.birthday": "2020-03-12",
        "ebizAppntDto.occupationCode": "A01",
        "ebizOrderInsuranceDto.cvaliDate": "2020-05-21",
        "ebizOrderInsuranceDto.insuYear": endDate,
        "thirdUserType": "",
        "orderRiskDutyList": '[' + stringProjects + ']'
    }
    return new Promise(function (resolve, reject) {
        request({
            url: url,
            method: "POST",
            json: false,
            rejectUnauthorized: false,
            headers: {
                "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.113 Safari/537.36"
            },
            body: helper.urlencode(params)
        }, function (err, res, body) {
            if (!err && res.statusCode == 200) {
                rs = {
                    "name": "安顺畅游个人意外保障（随心定制）",
                    "company": "安联保险集团",
                    "price": body.toString()
                }
                resolve(rs);
            }

        })

    })

}
var main = async function () {
    data = genProductInfo();
    for (i = 0; i < data.length; i++) {
        dt = data[i]
        rs = await httpRequest(dt);
        result = Object.assign( dt,rs )
        mysql.insertData( result );
        
    }
    mysql.close();

}
